/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.jpa;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;

import rad.framework.seam.ComponentUtil;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.persistence.ManagedPersistenceContext;

/**
 * @author boudyacho
 */
@Name("rad.framework.entityPersistenceContextMap")
@Scope(ScopeType.APPLICATION)
@AutoCreate
@BypassInterceptors
public class EntityPersistenceContextMap {

	@Logger
	private Log log;

	private Map<String, String> entityPersistenceContextNameMap;

	public String getPersistenceContextNameFor(Class<?> type) {
		return getPersistenceContextNameFor(type.getName());
	}

	public String getPersistenceContextNameFor(String name) {
		return getEntityPersistenceContextNameMap().get(name);
	}

	public Set<String> getEntityNames() {
		return getEntityPersistenceContextNameMap().keySet();
	}

	public Map<String, String> getEntityPersistenceContextNameMap() {
		if (entityPersistenceContextNameMap == null) {
			entityPersistenceContextNameMap = new HashMap<String, String>();
			
			List<Component> list = ComponentUtil
					.getComponentsByBeanClass(ManagedPersistenceContext.class);
			for (Component component : list) {
				String persistenceContextName = component.getName();
				EntityManager entityManager = (EntityManager) Component
						.getInstance(persistenceContextName);
				Set<String> entityNames = getEntityNames(entityManager);
				for (String name : entityNames) {
					entityPersistenceContextNameMap.put(name,
							persistenceContextName);
				}
			}

			log.debug("EntityPersistenceContextMap Initialized:");
			log.debug(this);
		}
		return entityPersistenceContextNameMap;
	}

	public EntityManager getEntityManagerNameFor(Class<?> type) {
		String pcn = getPersistenceContextNameFor(type.getName());
		return (EntityManager) Component.getInstance(pcn);
	}

	protected Set<String> getEntityNames(EntityManager entityManager) {
		try {
			return JpaUtil.getEntityNames(entityManager);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public String toString() {
		ToStringBuilder builder = new ToStringBuilder(this,
				ToStringStyle.MULTI_LINE_STYLE);
		Set<String> names = entityPersistenceContextNameMap.keySet();
		for (String name : names) {
			builder.append(name, entityPersistenceContextNameMap.get(name));
		}
		return builder.toString();
	}

	public static EntityPersistenceContextMap instance() {
		return (EntityPersistenceContextMap) Component
				.getInstance(EntityPersistenceContextMap.class);
	}
}
